home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / pc / server / mmserv.exe / htmllib.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-21  |  19.7 KB  |  559 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. from sgmllib import SGMLParser
  5. from formatter import AS_IS
  6. __all__ = [
  7.     'HTMLParser']
  8.  
  9. class HTMLParser(SGMLParser):
  10.     from htmlentitydefs import entitydefs
  11.     
  12.     def __init__(self, formatter, verbose = 0):
  13.         SGMLParser.__init__(self, verbose)
  14.         self.formatter = formatter
  15.  
  16.     
  17.     def reset(self):
  18.         SGMLParser.reset(self)
  19.         self.savedata = None
  20.         self.isindex = 0
  21.         self.title = None
  22.         self.base = None
  23.         self.anchor = None
  24.         self.anchorlist = []
  25.         self.nofill = 0
  26.         self.list_stack = []
  27.  
  28.     
  29.     def handle_data(self, data):
  30.         if self.savedata is not None:
  31.             self.savedata = self.savedata + data
  32.         elif self.nofill:
  33.             self.formatter.add_literal_data(data)
  34.         else:
  35.             self.formatter.add_flowing_data(data)
  36.  
  37.     
  38.     def save_bgn(self):
  39.         self.savedata = ''
  40.  
  41.     
  42.     def save_end(self):
  43.         data = self.savedata
  44.         self.savedata = None
  45.         if not (self.nofill):
  46.             data = ' '.join(data.split())
  47.         
  48.         return data
  49.  
  50.     
  51.     def anchor_bgn(self, href, name, type):
  52.         self.anchor = href
  53.         if self.anchor:
  54.             self.anchorlist.append(href)
  55.         
  56.  
  57.     
  58.     def anchor_end(self):
  59.         if self.anchor:
  60.             self.handle_data('[%d]' % len(self.anchorlist))
  61.             self.anchor = None
  62.         
  63.  
  64.     
  65.     def handle_image(self, src, alt, *args):
  66.         self.handle_data(alt)
  67.  
  68.     
  69.     def start_html(self, attrs):
  70.         pass
  71.  
  72.     
  73.     def end_html(self):
  74.         pass
  75.  
  76.     
  77.     def start_head(self, attrs):
  78.         pass
  79.  
  80.     
  81.     def end_head(self):
  82.         pass
  83.  
  84.     
  85.     def start_body(self, attrs):
  86.         pass
  87.  
  88.     
  89.     def end_body(self):
  90.         pass
  91.  
  92.     
  93.     def start_title(self, attrs):
  94.         self.save_bgn()
  95.  
  96.     
  97.     def end_title(self):
  98.         self.title = self.save_end()
  99.  
  100.     
  101.     def do_base(self, attrs):
  102.         for a, v in attrs:
  103.             if a == 'href':
  104.                 self.base = v
  105.                 continue
  106.         
  107.  
  108.     
  109.     def do_isindex(self, attrs):
  110.         self.isindex = 1
  111.  
  112.     
  113.     def do_link(self, attrs):
  114.         pass
  115.  
  116.     
  117.     def do_meta(self, attrs):
  118.         pass
  119.  
  120.     
  121.     def do_nextid(self, attrs):
  122.         pass
  123.  
  124.     
  125.     def start_h1(self, attrs):
  126.         self.formatter.end_paragraph(1)
  127.         self.formatter.push_font(('h1', 0, 1, 0))
  128.  
  129.     
  130.     def end_h1(self):
  131.         self.formatter.end_paragraph(1)
  132.         self.formatter.pop_font()
  133.  
  134.     
  135.     def start_h2(self, attrs):
  136.         self.formatter.end_paragraph(1)
  137.         self.formatter.push_font(('h2', 0, 1, 0))
  138.  
  139.     
  140.     def end_h2(self):
  141.         self.formatter.end_paragraph(1)
  142.         self.formatter.pop_font()
  143.  
  144.     
  145.     def start_h3(self, attrs):
  146.         self.formatter.end_paragraph(1)
  147.         self.formatter.push_font(('h3', 0, 1, 0))
  148.  
  149.     
  150.     def end_h3(self):
  151.         self.formatter.end_paragraph(1)
  152.         self.formatter.pop_font()
  153.  
  154.     
  155.     def start_h4(self, attrs):
  156.         self.formatter.end_paragraph(1)
  157.         self.formatter.push_font(('h4', 0, 1, 0))
  158.  
  159.     
  160.     def end_h4(self):
  161.         self.formatter.end_paragraph(1)
  162.         self.formatter.pop_font()
  163.  
  164.     
  165.     def start_h5(self, attrs):
  166.         self.formatter.end_paragraph(1)
  167.         self.formatter.push_font(('h5', 0, 1, 0))
  168.  
  169.     
  170.     def end_h5(self):
  171.         self.formatter.end_paragraph(1)
  172.         self.formatter.pop_font()
  173.  
  174.     
  175.     def start_h6(self, attrs):
  176.         self.formatter.end_paragraph(1)
  177.         self.formatter.push_font(('h6', 0, 1, 0))
  178.  
  179.     
  180.     def end_h6(self):
  181.         self.formatter.end_paragraph(1)
  182.         self.formatter.pop_font()
  183.  
  184.     
  185.     def do_p(self, attrs):
  186.         self.formatter.end_paragraph(1)
  187.  
  188.     
  189.     def start_pre(self, attrs):
  190.         self.formatter.end_paragraph(1)
  191.         self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
  192.         self.nofill = self.nofill + 1
  193.  
  194.     
  195.     def end_pre(self):
  196.         self.formatter.end_paragraph(1)
  197.         self.formatter.pop_font()
  198.         self.nofill = max(0, self.nofill - 1)
  199.  
  200.     
  201.     def start_xmp(self, attrs):
  202.         self.start_pre(attrs)
  203.         self.setliteral('xmp')
  204.  
  205.     
  206.     def end_xmp(self):
  207.         self.end_pre()
  208.  
  209.     
  210.     def start_listing(self, attrs):
  211.         self.start_pre(attrs)
  212.         self.setliteral('listing')
  213.  
  214.     
  215.     def end_listing(self):
  216.         self.end_pre()
  217.  
  218.     
  219.     def start_address(self, attrs):
  220.         self.formatter.end_paragraph(0)
  221.         self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
  222.  
  223.     
  224.     def end_address(self):
  225.         self.formatter.end_paragraph(0)
  226.         self.formatter.pop_font()
  227.  
  228.     
  229.     def start_blockquote(self, attrs):
  230.         self.formatter.end_paragraph(1)
  231.         self.formatter.push_margin('blockquote')
  232.  
  233.     
  234.     def end_blockquote(self):
  235.         self.formatter.end_paragraph(1)
  236.         self.formatter.pop_margin()
  237.  
  238.     
  239.     def start_ul(self, attrs):
  240.         self.formatter.end_paragraph(not (self.list_stack))
  241.         self.formatter.push_margin('ul')
  242.         self.list_stack.append([
  243.             'ul',
  244.             '*',
  245.             0])
  246.  
  247.     
  248.     def end_ul(self):
  249.         if self.list_stack:
  250.             del self.list_stack[-1]
  251.         
  252.         self.formatter.end_paragraph(not (self.list_stack))
  253.         self.formatter.pop_margin()
  254.  
  255.     
  256.     def do_li(self, attrs):
  257.         self.formatter.end_paragraph(0)
  258.         if self.list_stack:
  259.             (dummy, label, counter) = top = self.list_stack[-1]
  260.             top[2] = counter = counter + 1
  261.         else:
  262.             (label, counter) = ('*', 0)
  263.         self.formatter.add_label_data(label, counter)
  264.  
  265.     
  266.     def start_ol(self, attrs):
  267.         self.formatter.end_paragraph(not (self.list_stack))
  268.         self.formatter.push_margin('ol')
  269.         label = '1.'
  270.         for a, v in attrs:
  271.             if a == 'type':
  272.                 if len(v) == 1:
  273.                     v = v + '.'
  274.                 
  275.                 label = v
  276.                 continue
  277.         
  278.         self.list_stack.append([
  279.             'ol',
  280.             label,
  281.             0])
  282.  
  283.     
  284.     def end_ol(self):
  285.         if self.list_stack:
  286.             del self.list_stack[-1]
  287.         
  288.         self.formatter.end_paragraph(not (self.list_stack))
  289.         self.formatter.pop_margin()
  290.  
  291.     
  292.     def start_menu(self, attrs):
  293.         self.start_ul(attrs)
  294.  
  295.     
  296.     def end_menu(self):
  297.         self.end_ul()
  298.  
  299.     
  300.     def start_dir(self, attrs):
  301.         self.start_ul(attrs)
  302.  
  303.     
  304.     def end_dir(self):
  305.         self.end_ul()
  306.  
  307.     
  308.     def start_dl(self, attrs):
  309.         self.formatter.end_paragraph(1)
  310.         self.list_stack.append([
  311.             'dl',
  312.             '',
  313.             0])
  314.  
  315.     
  316.     def end_dl(self):
  317.         self.ddpop(1)
  318.         if self.list_stack:
  319.             del self.list_stack[-1]
  320.         
  321.  
  322.     
  323.     def do_dt(self, attrs):
  324.         self.ddpop()
  325.  
  326.     
  327.     def do_dd(self, attrs):
  328.         self.ddpop()
  329.         self.formatter.push_margin('dd')
  330.         self.list_stack.append([
  331.             'dd',
  332.             '',
  333.             0])
  334.  
  335.     
  336.     def ddpop(self, bl = 0):
  337.         self.formatter.end_paragraph(bl)
  338.         if self.list_stack:
  339.             if self.list_stack[-1][0] == 'dd':
  340.                 del self.list_stack[-1]
  341.                 self.formatter.pop_margin()
  342.             
  343.         
  344.  
  345.     
  346.     def start_cite(self, attrs):
  347.         self.start_i(attrs)
  348.  
  349.     
  350.     def end_cite(self):
  351.         self.end_i()
  352.  
  353.     
  354.     def start_code(self, attrs):
  355.         self.start_tt(attrs)
  356.  
  357.     
  358.     def end_code(self):
  359.         self.end_tt()
  360.  
  361.     
  362.     def start_em(self, attrs):
  363.         self.start_i(attrs)
  364.  
  365.     
  366.     def end_em(self):
  367.         self.end_i()
  368.  
  369.     
  370.     def start_kbd(self, attrs):
  371.         self.start_tt(attrs)
  372.  
  373.     
  374.     def end_kbd(self):
  375.         self.end_tt()
  376.  
  377.     
  378.     def start_samp(self, attrs):
  379.         self.start_tt(attrs)
  380.  
  381.     
  382.     def end_samp(self):
  383.         self.end_tt()
  384.  
  385.     
  386.     def start_strong(self, attrs):
  387.         self.start_b(attrs)
  388.  
  389.     
  390.     def end_strong(self):
  391.         self.end_b()
  392.  
  393.     
  394.     def start_var(self, attrs):
  395.         self.start_i(attrs)
  396.  
  397.     
  398.     def end_var(self):
  399.         self.end_i()
  400.  
  401.     
  402.     def start_i(self, attrs):
  403.         self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
  404.  
  405.     
  406.     def end_i(self):
  407.         self.formatter.pop_font()
  408.  
  409.     
  410.     def start_b(self, attrs):
  411.         self.formatter.push_font((AS_IS, AS_IS, 1, AS_IS))
  412.  
  413.     
  414.     def end_b(self):
  415.         self.formatter.pop_font()
  416.  
  417.     
  418.     def start_tt(self, attrs):
  419.         self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
  420.  
  421.     
  422.     def end_tt(self):
  423.         self.formatter.pop_font()
  424.  
  425.     
  426.     def start_a(self, attrs):
  427.         href = ''
  428.         name = ''
  429.         type = ''
  430.         for attrname, value in attrs:
  431.             value = value.strip()
  432.             if attrname == 'href':
  433.                 href = value
  434.             
  435.             if attrname == 'name':
  436.                 name = value
  437.             
  438.             if attrname == 'type':
  439.                 type = value.lower()
  440.                 continue
  441.         
  442.         self.anchor_bgn(href, name, type)
  443.  
  444.     
  445.     def end_a(self):
  446.         self.anchor_end()
  447.  
  448.     
  449.     def do_br(self, attrs):
  450.         self.formatter.add_line_break()
  451.  
  452.     
  453.     def do_hr(self, attrs):
  454.         self.formatter.add_hor_rule()
  455.  
  456.     
  457.     def do_img(self, attrs):
  458.         align = ''
  459.         alt = '(image)'
  460.         ismap = ''
  461.         src = ''
  462.         width = 0
  463.         height = 0
  464.         for attrname, value in attrs:
  465.             if attrname == 'align':
  466.                 align = value
  467.             
  468.             if attrname == 'alt':
  469.                 alt = value
  470.             
  471.             if attrname == 'ismap':
  472.                 ismap = value
  473.             
  474.             if attrname == 'src':
  475.                 src = value
  476.             
  477.             if attrname == 'width':
  478.                 
  479.                 try:
  480.                     width = int(value)
  481.                 except ValueError:
  482.                     pass
  483.                 except:
  484.                     None<EXCEPTION MATCH>ValueError
  485.                 
  486.  
  487.             None<EXCEPTION MATCH>ValueError
  488.             if attrname == 'height':
  489.                 
  490.                 try:
  491.                     height = int(value)
  492.                 except ValueError:
  493.                     pass
  494.                 except:
  495.                     None<EXCEPTION MATCH>ValueError
  496.                 
  497.  
  498.             None<EXCEPTION MATCH>ValueError
  499.         
  500.         self.handle_image(src, alt, ismap, align, width, height)
  501.  
  502.     
  503.     def do_plaintext(self, attrs):
  504.         self.start_pre(attrs)
  505.         self.setnomoretags()
  506.  
  507.     
  508.     def unknown_starttag(self, tag, attrs):
  509.         pass
  510.  
  511.     
  512.     def unknown_endtag(self, tag):
  513.         pass
  514.  
  515.  
  516.  
  517. def test(args = None):
  518.     import sys
  519.     import formatter
  520.     if not args:
  521.         args = sys.argv[1:]
  522.     
  523.     if args:
  524.         pass
  525.     silent = args[0] == '-s'
  526.     if silent:
  527.         del args[0]
  528.     
  529.     if args:
  530.         file = args[0]
  531.     else:
  532.         file = 'test.html'
  533.     if file == '-':
  534.         f = sys.stdin
  535.     else:
  536.         
  537.         try:
  538.             f = open(file, 'r')
  539.         except IOError:
  540.             msg = None
  541.             print file, ':', msg
  542.             sys.exit(1)
  543.  
  544.     data = f.read()
  545.     if f is not sys.stdin:
  546.         f.close()
  547.     
  548.     if silent:
  549.         f = formatter.NullFormatter()
  550.     else:
  551.         f = formatter.AbstractFormatter(formatter.DumbWriter())
  552.     p = HTMLParser(f)
  553.     p.feed(data)
  554.     p.close()
  555.  
  556. if __name__ == '__main__':
  557.     test()
  558.  
  559.